package com.ruoyi.gd.fj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.fj.domain.ZFujian;
import com.ruoyi.gd.fj.domain.bo.ZFujianBo;
import com.ruoyi.gd.fj.domain.vo.ZFujianVo;
import com.ruoyi.gd.fj.mapper.ZFujianMapper;
import com.ruoyi.gd.fj.service.IZFujianService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 附件Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-17
 */
@RequiredArgsConstructor
@Service
public class ZFujianServiceImpl implements IZFujianService {

    @Value("${upload.path}")
    private String uploadPath;


    private final ZFujianMapper baseMapper;

    /**
     * 查询附件
     */
    @Override
    public ZFujianVo queryById(String fjid){
        return baseMapper.selectVoById(fjid);
    }

    /**
     * 查询附件列表
     */
    @Override
    public TableDataInfo<ZFujianVo> queryPageList(ZFujianBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZFujian> lqw = buildQueryWrapper(bo);
        Page<ZFujianVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询附件列表
     */
    @Override
    public List<ZFujianVo> queryList(ZFujianBo bo) {
        LambdaQueryWrapper<ZFujian> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZFujian> buildQueryWrapper(ZFujianBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZFujian> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFjname()), ZFujian::getFjname, bo.getFjname());
        lqw.eq(StringUtils.isNotBlank(bo.getFjtype()), ZFujian::getFjtype, bo.getFjtype());
        lqw.eq(StringUtils.isNotBlank(bo.getFjpath()), ZFujian::getFjpath, bo.getFjpath());
        if (StringUtils.isNotEmpty(bo.getFjid())) {
            String[] fjidArr = bo.getFjid().split(",");
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < fjidArr.length; i++) {
                list.add(fjidArr[i]);
            }
            lqw.in(ZFujian::getFjid, list);
        }
        return lqw;
    }

    /**
     * 新增附件
     */
    @Override
    public Boolean insertByBo(ZFujianBo bo) {
        ZFujian add = BeanUtil.toBean(bo, ZFujian.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFjid(add.getFjid());
        }
        return flag;
    }

    /**
     * 修改附件
     */
    @Override
    public Boolean updateByBo(ZFujianBo bo) {
        ZFujian update = BeanUtil.toBean(bo, ZFujian.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZFujian entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        // 如果当前的附件数据删除或则删除附件
//        for (String id : ids) {
//            ZFujianVo zFujianVo = queryById(id);
//            Path filePath = Paths.get(uploadPath, zFujianVo.getFjpath());
//            if (Files.exists(filePath)) {
//                // 删除文件
//                try {
//                    Files.delete(filePath);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ZFujianVo> queryListByIdOrdery(String ids) {
        return baseMapper.queryListByIdOrdery(ids);
    }

    @Override
    public List<ZFujianVo> queryListByIds(List<String> collect) {
        List<ZFujianVo> zFujianVos = baseMapper.selectVoBatchIds(collect);
        return zFujianVos;
    }

}
